package com.bbtShop.core.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbtShop.core.model.RegionDto;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @author : manke
 * @Description
 * @Date: 2020/11/3 20:54
 */
@Component
public class IpToAddrUtil {
    @Value("${map.ip.url}")
    private String url;
    @Value("${map.ip.ak}")
    private String ak;

    public  RegionDto Addr(String ip){

        HashMap<String, String> map = new HashMap<>();
        map.put("ak", ak);
//                map.put("ip", "60.175.245.17");
        map.put("ip", ip.equals("127.0.0.1") ? "60.175.245.17" : ip);
        String res = HttpClientUtil.doGet(url, map);
        JSONObject jsonObject = JSON.parseObject(res);
        RegionDto regionDto = new RegionDto();
        regionDto.setProvince("未知");
        regionDto.setCity("未知");
        String state = jsonObject.get("status").toString();
        if ("0".equals(state)) {
            JSONObject re = jsonObject.getJSONObject("content").getJSONObject("address_detail");
            regionDto.setProvince((String) re.get("province"));
            regionDto.setCity((String) re.get("city"));
        }
        return regionDto;

    }

}
